layui.use(['layer','form'], function() {
    var layer = layui.layer, form = layui.form;
    var id = getUrlParam('id');
    var fileObj = {docId:fileId, docType:'overseas_receiving_account'};
    $('.file-size').attr({'doc-id':id});
    fn = {
        initSubject:function (overseasId) {
            show_loading();
            $.get('/scm/overseasReceivingAccount/subjectSelect',function (res) {
                if(isSuccess(res)){
                    var $subjectId = $('#subjectId');
                    $.each(res.data,function (i,o) {
                        $subjectId.append('<option value="'+o.id+'_'+o.type+'" '+(o.id==overseasId?'selected':'')+'>'+o.name+'</option>');
                    });
                    form.render('select');
                    fn.renderOverseasBank();
                    autoCurrency();
                }
            });
        },
        renderOverseasBank:function () {
            var params = {
                subjectType:$('#subjectId').val().split('_')[1],
                overseasId:$('#subjectId').val().split('_')[0],
                overseasName:$('#subjectId option:selected').text()
            }
            form.val('form',params);
            var bankUrl = '/scm/subjectBank/list';
            if('abroad'==params.subjectType){
                bankUrl = '/scm/subjectOverseasBank/selectByOverseasId'
            }
            show_loading();
            var $overseasBankId=$('#overseasBankId');
            $overseasBankId.get(0).length = 0;
            $.post(bankUrl,{overseasId:params.overseasId},function (res) {
                if(isSuccess(res)){
                    $.each(res.data,function (i,o) {
                        $overseasBankId.append('<option value="'+o.id+'">'+o.name+'（'+o.account+'）</option>');
                    });
                    form.render('select');
                }
            });
        }
    };
    $('#fileId').val(id);
    initFileSize();
    show_loading();
    $.get(rurl('scm/overseasReceivingAccount/detail'),{'id':id,'operation':'overseas_receiving_account_edit'},function (res) {
        if(isSuccessWarnClose(res)){
            var data = res.data.account;
            form.val('form',data);
            fn.initSubject(data.overseasId);
        }
    });

    form.on('select(subjectId)',function (data) {
        fn.renderOverseasBank();
    });
    form.on('submit(save-btn)', function(data){
        formSub('scm/overseasReceivingAccount/edit',function (res) {
            parent.reloadList();
            closeThisWin('修改成功');
        });
        return false;
    });
    $('.auto-supplier-bank').autocomplete({
        source: function (request, response) {
            $.post(rurl('scm/supplierBank/vague'),{customerId:$('#customerId').val(),supplierName:$('#payer').val(),bankName: trim(request.term)},function (res) {
                response($.map(res.data, function (item) {return {label: item.name, value: item.name, data: item}}));
            });
        }, minLength: 0,autoFocus:true, delay: 300,
        select: function( event, ui ) {
            var data = ui.item.data;
            form.val("form",{'payerBankNo':data.account});
        }
    }).focus(function () {
        $(this).select().autocomplete("search");
    });
});
function fullCustomerData(data) {
    $('#customerId').val(data.id);
}